<?php
namespace app\common\component\user;

class UserValidate extends \think\Validate {
	public function common() {
		$this -> rule([
			'username' => 'require',
			'password' => 'require|min:6'
		]);

		$this -> message([
			'username.require' => '账号必须填写',
			'password.require' => '密码必须填写',
			'password.min' => '密码必须大于六位'
		]);
	}
	
	public function checkPassword($data) {
		$this -> rule([
			'old_password' => 'require|min:6',
			'new_password' => 'require|min:6',
			'new_confirm_password' => 'require|min:6|confirm:new_password'
		]);

		$this -> message([
			'old_password.require' => '原密码必须填写',
			'old_password.min' => '原密码必须大于六位',
			'new_password.require' => '新密码必须填写',
			'new_password.min' => '新密码必须大于六位',
			'new_confirm_password.require' => '新确认密码必须填写',
			'new_confirm_password.min' => '新确认密码必须大于六位',
			'new_confirm_password.confirm' => '新密码与新确认密码不一致'
		]);

		$getUser = (new UserService) -> getUser();

		if(!checkPasswordHash($data['old_password'], $getUser['data']['user'] -> password)) {
			return [
				'code' => -1,
				'message' => '原密码错误!'
			];
		}

		return $this -> checkData($data);
	}
	
	public function checkPayPassowrd($data) {
		$this -> rule([
			'old_pay_password' => 'require|min:6',
			'new_pay_password' => 'require|min:6',
			'new_pay_confirm_password' => 'require|min:6|confirm:new_pay_password'
		]);

		$this -> message([
			'old_pay_password.require' => '原安全密码必须填写',
			'old_pay_password.min' => '原安全密码必须大于六位',
			'new_pay_password.require' => '新安全密码必须填写',
			'new_pay_password.min' => '新安全密码必须大于六位',
			'new_pay_confirm_password.require' => '新确认安全密码必须填写',
			'new_pay_confirm_password.min' => '新确认安全密码必须大于六位',
			'new_pay_confirm_password.confirm' => '新安全密码与新确认安全密码不一致'
		]);

		$getUser = (new UserService) -> getUser();

		if(!checkPasswordHash($data['old_pay_password'], $getUser['data']['user'] -> pay_password)) {
			return [
				'code' => -1,
				'message' => '原安全密码错误!'
			];
		}

		return $this -> checkData($data);
	}
	
	public function login($data) {
		$this -> common();

		$this -> rule([
			'captcha' => 'require|captcha'
		]);

		$this -> message([
			'captcha.require' => '验证码必须填写'
		]);

    	return $this -> checkData($data);
	}

	public function create($data) {
		$this -> common();

		$this -> rule([
			'phone' => 'require|length:11',
			'realname' => 'require',
			'bank_code' => 'require|confirm:phone',
			'comfirm_password' => 'require|confirm:password',
			'pay_password' => 'require',
			'comfirm_pay_password' => 'require|confirm:pay_password',
			'captcha' => 'require|captcha'
		]);

		$this -> message([
			'phone.require' => '手机必须填写',
			'phone.length' => '请填写正确的手机号',
			'realname.require' => '真实姓名必须填写',
			'bank_code.length' => '云付通账号必须填写',
			'bank_code.confirm' => '手机与云付通账号必须一致',
			'comfirm_password.require' => '确认密码必须填写',
			'comfirm_password.confirm' => '密码与确认密码不一致',
			'pay_password.require' => '安全密码必须填写',
			'comfirm_pay_password.require' => '确认安全密码必须填写',
			'comfirm_pay_password.confirm' => '安全密码与确认安全密码不一致',
			'captcha.require' => '验证码必须填写'
		]);

		if($this -> check($data)) {
			if(UserModel::get(['username' => $data['username']])) {
	    		return [
					'code' => -1,
					'message' => '此账号:' . $data['username'] . '已被注册!'
				];
	    	}

	    	if(UserModel::get(['phone' => $data['phone']])) {
	    		return [
					'code' => -1,
					'message' => '此手机号:' . $data['phone'] . '已被注册!'
				];
	    	}

			/*
	    	if(UserModel::get(['bank_code' => $data['bank_code']])) {
	    		return [
					'code' => -1,
					'message' => '此云付通:' . $data['bank_code'] . '已被注册!'
				];
	    	}
	    	*/

    		return [
    			'code' => 0,
    			'message' => 'Success'
    		];
    	}
    	return [
			'code' => -1,
			'message' => $this -> getError()
		];
	}
	
	public function update($data) {
		$this -> rule([
			'realname' => 'require',
			'sex' => 'require|in:0,1',
			'phone' => 'require'
		]);

		$this -> message([
			'realname.require' => '真实姓名必须填写',
			'sex.require' => '性别必须选择',
			'phone.require' => '手机必须填写'
		]);
		
		return $this -> checkData($data);
	}
	
	public function checkData($data) {
		if($this -> check($data)) {
    		return [
				'code' => 0,
				'message' => 'Success'
			];
    	}

    	return [
			'code' => -1,
			'message' => $this -> getError()
		];
	}
}
?>